﻿using System.Windows;
using Microsoft.Phone.Controls;
using Samples.Radio.Agent;
using Samples.Radio.Data;
using Samples.Radio.Helpers;

namespace Samples.Radio.Pages
{
    public class AudioBasePage:PhoneApplicationPage {

        void ViewModelStateChanged(object sender, DualParameterEventArgs<string, bool> e)
        {
            try
            {
                VisualStateManager.GoToState(this, e.Parameter1, e.Parameter2);
            }
            catch
            {
                // Intercept any exceptions - if attempt to change state between
                // page navigations an exception can be raised!
            }
        }



        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            var viewModel = (DataContext as ViewModelBase);
            if (viewModel == null) return;
    
            viewModel.StateChanged += ViewModelStateChanged;
            viewModel.OnNavigatedTo(e,NavigationContext,State);
    
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);
            var viewModel = (DataContext as ViewModelBase);
            if (viewModel == null) return;

            viewModel.OnNavigatingFrom(e, State);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            var viewModel = (DataContext as ViewModelBase);
            if (viewModel == null) return;

            viewModel.OnNavigatedFrom(e, State);
            viewModel.StateChanged -= ViewModelStateChanged;
        }
    }
}
